//
//  CarsViewController.m
//  FuelLog
//
//  Created by Pure Mass Communications on 9/1/08.
//  Copyright 2008 . All rights reserved.
//
//  Placed under GPL v2 with the kind permission of Pure Mass Communications
//
//  Modified to make compile under SDK 3.1 by Olaf Greck in May 2010
//
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#import "CarsViewController.h"
#import "FuelLogAppDelegate.h"
#import "FuelLogViewController.h"
#import "AddCarViewController.h"
#import "LoginViewController.h"
#import "CarCell.h"

//#import "Car.h" // needed in 3.0 for accessoryButtonTappedForRowWithIndexPath ?
#import "DebugOutput.h"

@implementation CarsViewController
@synthesize tableView;
@synthesize importButton;

/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		// Initialization code
	}
	return self;
}
*/
 
/*
 Implement loadView if you want to create a view hierarchy programmatically
- (void)loadView {
}
 */


- (void)viewDidLoad {
 [self.importButton useSystemTouchStyle];  
 
}
 
#pragma mark -
- (void)viewWillAppear:(BOOL)animated {
    [self.tableView reloadData];
	[super setEditing:YES animated:NO];
		debug(@"in viewWillAppear");
	

}
 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
	// Release anything that's not essential, such as cached data
}

- (void)dealloc {
	[tableView release];
	[importButton release];
	[super dealloc];
}

#pragma mark -
-(IBAction)addCar:(id)sender {
	AddCarViewController *addController = [FuelLogViewController addCarViewController];
	Car *car = [[Car alloc] init];
	[addController setCar:car];
	[addController setLabel];
	[self presentModalViewController:addController animated:YES];
}

-(IBAction)chooseCar:(id)sender {
	[self dismissModalViewControllerAnimated:YES];
}

-(IBAction)importCar:(id)sender {
	debug(@"in importCar");
	[self presentModalViewController:[FuelLogViewController filesViewController] animated:YES];
}
 
#pragma mark -
#pragma mark - UITableView delegate methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tv {
	return 1;
}

- (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section {
    FuelLogAppDelegate *appDelegate = (FuelLogAppDelegate *)[[UIApplication sharedApplication] delegate];
    return appDelegate.cars.count;
}

- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	// first make uncheck all cells
	NSArray *cells = [tv visibleCells];
	for(int i = 0; i < cells.count; ++i) {
		CarCell *theCell = (CarCell *)[cells objectAtIndex:i];
		[theCell uncheck];
	}
	CarCell *carCell = (CarCell *)[tv cellForRowAtIndexPath:indexPath];
	[carCell checkAction:nil];
	[tv deselectRowAtIndexPath:indexPath animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CarCell *carCell = (CarCell *)[tableView dequeueReusableCellWithIdentifier:@"CarCell"];
    if (carCell == nil) {
        // Create a new cell. CGRectZero allows the cell to determine the appropriate size.
//        carCell = [[[CarCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"CarCell"] autorelease];
        carCell = [[[CarCell alloc] initWithFrame:CGRectZero ] autorelease];
		carCell.master = self;
    }
    // Retrieve the car object matching the row from the application delegate's array.
    FuelLogAppDelegate *appDelegate = (FuelLogAppDelegate *)[[UIApplication sharedApplication] delegate];
    Car *car = (Car *)[appDelegate.cars objectAtIndex:indexPath.row];
	carCell.car = car;
    return carCell;
}

#pragma mark Another iPhone bug: the checked image shows up on the wrong row 

- (void)clickSelectedCar {
	debug(@"in clickSelectedCar");
	FuelLogAppDelegate *appDelegate = (FuelLogAppDelegate *)[[UIApplication sharedApplication] delegate];
	NSUInteger idx[] = {0, 0};
	int i;
	for (i=0; i<[appDelegate.cars count]; ++i) {
		if (((Car *)[appDelegate.cars objectAtIndex:i]).selected) {
			idx[1] = i;
			break;
		}
	}
	[self tableView:tableView didSelectRowAtIndexPath:[NSIndexPath indexPathWithIndexes:idx length:2]];
}

#pragma mark -
// OS 3.0+ only otherwise see - (void)onClick:(id)sender in CarCell.m
///*
 - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
	debug(@"in accessoryButtonTappedForRowWithIndexPath");
	FuelLogAppDelegate *appDelegate = (FuelLogAppDelegate *)[[UIApplication sharedApplication] delegate];
    Car *theCar = (Car *)[appDelegate.cars objectAtIndex:indexPath.row];
	AddCarViewController *addController = [FuelLogViewController addCarViewController];
	[theCar retain];
	[addController setCar:theCar];
	[addController setLabel];
	[self presentModalViewController:addController animated:YES];
}
// */
@end
